﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Networking;

public class Item : Entity, IInteractable
{
    public static HashSet<Item> all = new HashSet<Item>();
    public float cooldown = 0.1f;
    [Multiline]
    public string description = string.Empty;
    private bool destroyed;
    public int price = 2;
    private Rigidbody rb;
    public int sellingPrice = 1;
    private BoxCollider trigger;
    public AnimationClip useAnimation;
    public Effect useEffect;
    public Character user;
    public float weight;

    protected override void Awake()
    {
        base.Awake();
        this.rb = base.GetComponent<Rigidbody>();
        this.trigger = base.gameObject.AddComponent<BoxCollider>();
        this.trigger.isTrigger = true;
        this.trigger.center += (Vector3) (Vector3.up * 0.5f);
        base.gameObject.layer = GameLayers.Item.index;
        this.awakeTime = Time.time;
        this.lastDropTime = Time.time;
        all.Add(this);
    }

    public virtual bool CanInteract(Character target)
    {
        Life component = target.GetComponent<Life>();
        if ((component != null) && component.isDied)
        {
            return false;
        }
        return true;
    }

    public virtual bool CanPickup(Character picker)
    {
        return true;
    }

    public void DestroyItem()
    {
        if (!this.destroyed)
        {
            this.destroyed = true;
            UnityEngine.Object.Destroy(base.gameObject);
        }
    }

    private void DisableModelHover()
    {
        if ((this.rb == null) && (this.modelInstance != null))
        {
            AnimateHover component = this.modelInstance.gameObject.GetComponent<AnimateHover>();
            if (component != null)
            {
                component.Stop();
            }
            this.modelInstance.transform.localPosition = Vector3.zero;
            this.modelInstance.SetActive(false);
        }
    }

    public virtual void Drop(Vector3 position)
    {
        base.transform.parent = null;
        this.user = null;
        this.trigger.enabled = true;
        base.transform.position = position;
        if ((base.model != null) && (base.model.instance != null))
        {
            this.Unequip();
            if (this.rb != null)
            {
                this.rb.isKinematic = false;
                this.rb.velocity = new Vector3((float) UnityEngine.Random.Range(-5, 5), 0f, (float) UnityEngine.Random.Range(-5, 5));
            }
            this.EnableModelHover();
        }
        this.lastDropTime = Time.time;
    }

    private void EnableModelHover()
    {
        if ((this.rb == null) && ((this.modelInstance != null) && (base.gameObject.layer == GameLayers.Item.index)))
        {
            this.modelInstance.gameObject.SetActive(true);
            AnimateHover orAddComponent = this.modelInstance.gameObject.GetOrAddComponent<AnimateHover>();
            orAddComponent.floor = 0.5f;
            orAddComponent.ceil = 1f;
            orAddComponent.Play();
        }
    }

    public virtual void Equip(Transform modelParent)
    {
        this.DisableModelHover();
        if ((modelParent != null) && (this.modelInstance != null))
        {
            this.modelInstance.transform.parent = modelParent;
            this.modelInstance.ResetTransform();
            this.modelInstance.gameObject.SetActive(true);
        }
    }

    public void Interact(Character target)
    {
        Inventory inventory = target.inventory;
        if ((inventory != null) && this.CanPickup(target))
        {
            inventory.Pickup(this);
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        if ((base.model != null) && (base.model.instance != null))
        {
            UnityEngine.Object.Destroy(base.model.instance.gameObject);
        }
        this.Unequip();
        all.Remove(this);
    }

    protected virtual void OnPickup(Character user)
    {
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag;
        bool flag2 = base.OnSerialize(writer, forceAll);
        return (flag | flag2);
    }

    public override void OnStartClient()
    {
        base.OnStartClient();
        if (this.user != null)
        {
            this.Pickup(this.user);
        }
    }

    protected virtual void OnUse(Character user)
    {
    }

    public void Pickup(Character user)
    {
        base.transform.SetParentAndResetTransform(user.transform);
        this.user = user;
        if (this.rb != null)
        {
            this.rb.isKinematic = true;
        }
        if (this.trigger != null)
        {
            this.trigger.enabled = false;
        }
        this.DisableModelHover();
        this.OnPickup(user);
    }

    public void PlayUseAnimation()
    {
        if ((this.user != null) && (this.useAnimation != null))
        {
            this.user.PlayUseItemAnimation(this.useAnimation);
        }
    }

    public virtual void SpawnUseEffect()
    {
        if (this.useEffect != null)
        {
            this.useEffect.Spawn<Effect>(base.transform.position, base.transform.rotation);
        }
    }

    protected virtual void Start()
    {
        if (this.user == null)
        {
            this.EnableModelHover();
        }
    }

    public virtual void Unequip()
    {
        if (this.modelInstance != null)
        {
            this.modelInstance.transform.parent = base.transform;
            this.modelInstance.gameObject.SetActive(false);
        }
    }

    private void UNetVersion()
    {
    }

    public virtual void Use()
    {
        if (this.isActiveAndEnabled)
        {
            this.SpawnUseEffect();
            this.PlayUseAnimation();
            this.lastUseTime = Time.time;
            if (this.user != null)
            {
                this.OnUse(this.user);
            }
        }
    }

    public float awakeTime { get; private set; }

    public virtual bool canUse
    {
        get
        {
            if (this.isCoolingdown)
            {
                return false;
            }
            return true;
        }
    }

    Character IInteractable.target
    {
        get
        {
            return this.user;
        }
    }

    public bool isActiveAndEnabled
    {
        get
        {
            return ((this != null) && base.isActiveAndEnabled);
        }
    }

    public bool isCoolingdown
    {
        get
        {
            return ((Time.time - this.lastUseTime) <= this.cooldown);
        }
    }

    public float lastDropTime { get; private set; }

    public float lastUseTime { get; private set; }

    public GameObject modelInstance
    {
        get
        {
            return ((base.model == null) ? null : base.model.instance);
        }
    }
}

